package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.CheckgroupMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Override
    public void addSetmeal(Setmeal setmeal, Integer[] checkgroupIds) {
        Integer setmealId = setmeal.getId();
        if (setmealId!=null){
            baseMapper.deleteCheckgroupsBySetmealId(setmealId);
        }
        // saveOrUpdate 此方法主键值为null或者为0为插入,否则为更新 且会自动获取主键值
        saveOrUpdate(setmeal);

        setmealId = setmeal.getId();
        if (checkgroupIds!=null){
            for (Integer checkgroupId : checkgroupIds) {
                baseMapper.addSetmealWithCheckgroup(setmealId,checkgroupId);
            }
        }
    }

    @Override
    public Map findPage(QueryPageBean queryPageBean) {
        // 这里要写泛型的
        LambdaQueryWrapper<Setmeal> lambda = new QueryWrapper<Setmeal>().lambda();
        String queryString = queryPageBean.getQueryString();
        queryString=queryString==null?"":queryString;

        lambda.like(Setmeal::getName,queryString).or().like(Setmeal::getHelpCode,queryString);

        // 此方法用于分页查询 总记录数与结果集都已封装至结果
        Page<Setmeal> page = page(new Page<Setmeal>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), lambda);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",page.getTotal());
        map.put("list",page.getRecords());
        return map;
    }

    @Override
    public List<Setmeal> findAllSetmeal() {
        List<Setmeal> list = list();
        return list;
    }

    @Autowired
    CheckgroupMapper checkgroupMapper;

    @Override
    public Setmeal findSetMealDetail(Integer id) {
        Setmeal setmeal = baseMapper.selectById(id);
        List<CheckGroup> checkGroupList = baseMapper.findCheckgroupBySetmealId(id);
        for (CheckGroup checkGroup : checkGroupList) {
            Integer groupId = checkGroup.getId();
            List<CheckItem> checkItemList = checkgroupMapper.findCheckItemByCheckgroupId(groupId);
            checkGroup.setCheckItems(checkItemList);
        }
        setmeal.setCheckGroups(checkGroupList);
        return setmeal;
    }

    @Override
    public Setmeal findSetmealById(Integer id) {
        Setmeal setmeal = baseMapper.selectById(id);
        return setmeal;
    }
}
